﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VIP.Extension;

namespace VIP.Manager
{

    /// <summary>
    /// 面板的层级
    /// </summary>
    public enum PANEL_LAYER
    {
        Panel,
        Tip,
    }

    public class UIManager : VIPMonoSingleton<UIManager>
    {

        /// <summary>
        /// 画布
        /// 找到场景中的Canvas
        /// </summary>
        private GameObject canvas;

        /// <summary>
        /// 面板字典
        /// 用来维护面板
        /// </summary>
        private Dictionary<string, BasePanel> dic;

        /// <summary>
        /// 层级面板
        /// 通过枚举来找到面板的父物体（两个同一级不同层）
        /// </summary>
        private Dictionary<PANEL_LAYER, Transform> layerDic;

        private void Awake()
        {
            //设置为不是全局单例
            IsGolbal = false;
            InitLayer();
            dic = new Dictionary<string, BasePanel>();

        }

        /// <summary>
        /// 初始化层级
        /// </summary>
        private void InitLayer()
        {
            canvas = GameObject.Find("VIPCanvas");
            if (canvas.IsNull())
            {
                Debug.LogError("场景中没有VIPCanvas");
                return;
            }
            layerDic = new Dictionary<PANEL_LAYER, Transform>();
            foreach (PANEL_LAYER item in Enum.GetValues(typeof(PANEL_LAYER)))
            {
                string layerName = item.ToString();
                Transform trans = canvas.transform.Find(layerName);
                layerDic.Add(item, trans);
            }
        }

        /// <summary>
        /// 打开面板
        /// where T : BasePanel 说明只有OpenPanel才能调用这个方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public void OpenPanel<T>(string panelPath, params object[] args) where T : BasePanel
        {
            //已经打开
            string name = typeof(T).ToString();
            if (dic.ContainsKey(name))
            {
                return;
            }
            //面板脚本(这里多态典型的应用，子类对象指向父类的引用)
            BasePanel panel = canvas.AddComponent<T>();
            panel.Init(args);
            dic.Add(name, panel);
            //加载面板
            panelPath = (panelPath.IsNotEmpty() ? panelPath : panel.panelPath);
            GameObject panelObj = Resources.Load<GameObject>("UIPanel/" + panelPath);
            if (panelObj.IsNull())
            {
                Debug.LogError("没有找到通过" + panelPath + "路径找到物体");
                return;
            }
            panel.panelPath = panelPath;
            panel.panel = Instantiate(panelObj);
            Transform skinTrans = panel.panel.transform;
            PANEL_LAYER layer = panel.layer;
            Transform parent = layerDic[layer];
            skinTrans.SetParent(parent, false);
            //panel的生命周期
            panel.OnShow();
        }

        /// <summary>
        /// 关闭面板
        /// </summary>
        /// <param name="name"></param>
        public void ClosePanel(string name)
        {
            BasePanel panel = dic[name];
            if (panel.IsNull())
            {
                return;
            }
            panel.OnClose();
            dic.Remove(name);
            Destroy(panel.panel);
            Destroy(panel);
        }

        /// <summary>
        /// 获取面板
        /// </summary>
        /// <typeparam name="T">面板脚本</typeparam>
        /// <param name="name">面板名字</param>
        /// <returns></returns>
        public T GetPanel<T>(string name)
        {
            T panel = dic[name].GetComponent<T>();
            return panel;
        }

        /// <summary>
        /// 创建文本内容
        /// </summary>
        /// <param name="content"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public HitPanel MakeText(string content, float time)
        {
            HitPanel hitPanel = GetPanel<HitPanel>("HitPanel");
            hitPanel = hitPanel.MakeText(content, time);
            return hitPanel;
        }

    }

}